<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notepad</title>



    <!-- bootstrap -->
    <link rel="stylesheet" href="./assets/css/bootstrap.min.css" />
    <!-- 引入MUI -->
    <link rel="stylesheet" href="./assets/plugins/mui/css/mui.min.css" />

    <!-- 下拉刷新 上拉加载 -->
    <link rel="stylesheet" href="./assets/plugins/mescroll/mescroll.min.css">
    <script src="./assets/plugins/mescroll/mescroll.min.js"></script>

    <!-- swiper -->
    <link rel="stylesheet" href="./assets/plugins/swiper/swiper.min.css" />

    <!-- animate.css -->
    <link rel="stylesheet" href="./assets/css/animate.min.css" />

    
    <!-- 重置样式 -->
    <link rel="stylesheet" href="./assets/css/reset.css" />

    <!-- 公共样式 -->
    <link rel="stylesheet" href="./assets/css/common.css" />

    <!-- 页面独立的 -->
    <link rel="stylesheet" href="./assets/css/indexcopy.css" />
   
    
</head>
<body>
    <!-- 搜索 -->
    <div class="search wow fadeInUp">
        <div class="box">
            <div class="icon">
                <span class="glyphicon glyphicon-search"></span>
            </div>

            <input class="input" id="keyDowns" placeholder="请输入名字" />

            <button class="btn" id="select" size="mini" type="default">搜索</button>
        </div>
    </div>

       <!-- 分类 -->
    <div class="type wow fadeInUp">
        <div class="list">
            <div class="item active">全部</div>
            <div class="item">餐饮</div>
            <div class="item">家居</div>
            <div class="item">服装</div>
            <div class="item">生鲜</div>
            <div class="item">家纺</div>
            <div class="item">汽车</div>
            <div class="item">房产</div>
            <div class="item">建材</div>
            <div class="item">珠宝</div>
            <div class="item">箱包</div>
            <div class="item">母婴</div>
            <div class="item">医院</div>
        </div>
    </div>

    <!-- 列表 -->
    <div id="mescroll" class= "mescroll contact wow fadeInUp" data-wow-delay="100ms">
        <div class="list">
            
        </div>
    </div>

    <!-- 底部 -->
    <div class="tabbar">
        <div class="box">
            <a href="index.html" class="item">
                <span class="icon glyphicon glyphicon-home"></span>
                <span class="name">首页</span>
            </a>
            <a href="ContactAdd.html" class="item">
                <span class="icon glyphicon glyphicon-list-alt"></span>
                <span class="name">新建</span>
            </a>
            <a href="UserIndex.html" class="item">
                <span class="icon glyphicon glyphicon-user"></span>
                <span class="name">我的</span>
            </a>
        </div>
    </div>
</body>
</html>
<!-- jquery  -->
<script src="./assets/js/jquery.min.js"></script>

<!-- bootstrap -->
<script src="./assets/js/bootstrap.min.js"></script>

<!-- wow.js -->
<script src="./assets/js/wow.min.js"></script>

<!-- swiper -->
<script src="./assets/plugins/swiper/swiper.min.js"></script>

<!-- 引入mui的js文件 -->
<script src="./assets/plugins/mui/js/mui.min.js"></script>

<!-- 引入请求封装库 -->
<script src="./assets/plugins/axios/axios.min.js"></script>
<script src="./assets/plugins/axios/request.js"></script>
<!-- 引入cookie文件 -->
<script src="./assets/plugins/cookie.js"></script>
<!-- 引入login异常登录文件 -->
<script src="./assets/plugins/login.js"></script>

<script>
    //实例化wow.js
    new WOW().init();

    var business = cookie.get('business')?cookie.get('business'):"{}";
    business = JSON.parse(business);//获取data对象
    //默认的全部
    var typelist = [{id:0,name:'全部'}];
    var keydowns = '';
    var typeid = 0;
    //键盘按下和按钮点击的事件
    $("#keyDowns").keydown(function(e){
        // console.log(e.keyCode)
        if(e.keyCode==13){
            keydowns = $.trim($("#keyDowns").val()) ;
            console.log(keydowns);
            //重新调用下拉方法
            down()
        }
    });
    $("#select").click(function(){
        keydowns = $.trim($("#keyDowns").val()) ;
        console.log(keydowns);
        //重新调用下拉方法
        down()
    });
    //点击导航栏切换函数
    function SwitchNav(that){
        typeid = $(that).data('typeid')?$(that).data('typeid'):0;
        $(that).addClass("active").siblings().removeClass("active")
        //重新调用下拉方法
        down()
    };
    
    //创建 mescroll 对象
    var mescroll = new MeScroll("mescroll",{
        down:{
            auto: false, //是否在初始化完毕之后自动执行下拉回调callback; 默认true
            callback:down
        },
        up:{
            auto: true, //是否在初始化时以上拉加载的方式自动加载第一页数据; 默认false
            callback:up,
            //配置回到顶部按钮
            toTop:{ 
                duration: 600,
                src : "./assets/images/mescroll-totop.png", //默认滚动到1000px显示,可配置offset修改
                offset : 100
            }
        }
    });
    //下拉
    function down(){
        //重置上拉的状态
        mescroll.resetUpScroll()

        //将列表清空
        $(".contact .list").empty() 
    };
    //上拉 
    async function up(page){
        var num = page.num; 
        var size = page.size;
        var data = {
            page:num,
            limit:size,
            typeid:typeid,
            search:keydowns,
            busid:business.id
        }
        // 发起同步请求
        var result = await POST({
            url:'card/card/index',
            params:data
        })
        // console.log(result.code)
        if(result.code == 0)
        {
            mui.toast(result.msg, {duration: 1000})
            // 暂无更多数据
            mescroll.endBySize(0, 0)
            return false
        }
        var list = result.data.list
        var count = result.data.count

        //设置上拉加载状态
        mescroll.endBySize(list.length, count)
        FetchList(list)
    }
    //列表渲染
    function FetchList(list){
        for(var item of list){
            var temp = `<div class="item">
                <div class="content">
                    <h3 class="title">
                        <a href="ContactAdd.html">${item.nickname}--${item.gender_text}</a>
                    </h3>

                    <div class="info">
                        <div class="icon">
                            <image src="./assets/images/info.png"></image>
                        </div>
                        <div class="desc">
                            ${item.remark}--手机号码:${item.mobile}
                        </div>
                    </div>
                </div>
                
                <div class="call icon">
                    <button class="mui-btn mui-btn-primary" onclick="Edit(${item.id})">编辑</button>
                    <button class="mui-btn mui-btn-danger" onclick="Delete(${item.id})">删除</button>
                </div>
            </div>`
            $(".contact .list").append(temp)
        }
    };
    // 封装方法查询
    (async ()=>{

        var result = await POST({
            url:'card/card/type'
        })
        if(result.data.length > 0){
            //获取下拉表数据成功 数组合并
            typelist = typelist.concat(result.data);
        }
        if(typelist.length >0){
            var html = '';
            typelist.forEach((item,index)=>{
                if(index == 0){
                    html+=`<div class="item active" onclick="SwitchNav(this)" data-typeid="${item.id}">${item.name}</div>`
                }else{
                    html+=`<div class="item" onclick="SwitchNav(this)" data-typeid="${item.id}">${item.name}</div>`
                }
            })
            $(".type .list").html(html);
        }
        
    })();

    // 编辑
    function Edit(cardid){
        location.href = `ContactEdit.html?cardid=${cardid}`
    };
    //删除
    function Delete(cardid){
        mui.confirm("是否确认删除","删除提示框",['否','是'],async function(e){
            // console.log(e);
            if(e.index){
                //关闭所有的对话框
                mui.closePopups();

                // 封装数据
                var params = {
                    busid:business.id,
                    cardid:cardid
                }
                // 发送同步请求
                var result = await POST({
                    url:'card/card/del',
                    params,
                })
                if(result.code){
                    //删除成功!
                    mui.toast(result.msg);
                    down();
                    return false;
                }else{
                    //删除失败!
                    mui.toast(result.msg);
                    return false;
                }
            }
        });
    }



</script>